package com.yuyou.train.lesson.api;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.yuyou.train.lesson.service.ILessonClassService;
import com.yuyou.train.lesson.service.ILessonClassUserRelationService;
import com.yuyou.train.lesson.service.ILessonResourceRuleFrameService;
import com.yuyou.train.lesson.service.ILessonResourceRuleRelationService;
import com.yuyou.train.lesson.service.ILessonService;
import com.yuyou.train.service.api.lesson.entity.Lesson;
import com.yuyou.train.service.api.lesson.entity.LessonClass;
import com.yuyou.train.service.api.lesson.entity.LessonClassUserRelation;
import com.yuyou.train.service.api.lesson.entity.LessonResourceRuleFrame;
import com.yuyou.train.service.api.lesson.entity.LessonResourceRuleRelation;
import com.yuyou.train.service.api.lesson.service.LessonAPI;





@RestController
public class LessonAPIImpl implements  LessonAPI{

	@Autowired
	private ILessonResourceRuleFrameService lessonResourceRuleFrameService;
	
	@Autowired
	private ILessonResourceRuleRelationService lesResRulRelService;
	
	@Autowired
	private ILessonService lessonService;

	@Autowired
	private ILessonClassService lessonClassService;
	
	@Autowired
	private ILessonClassUserRelationService lesUseRelService;

	@Autowired
	private ILessonClassUserRelationService lessonClassUserRelationService;
	
	@Override
	public List<LessonResourceRuleRelation> findLesResRuleRel(
			@RequestBody LessonResourceRuleRelation record) {
		return lesResRulRelService.findLesResRuleRel(record);
	}

	@Override
	public LessonResourceRuleRelation selectLessonRelByLessonId(@RequestParam("lessonId") String lessonId) {
		return lesResRulRelService.selectByLessonId(lessonId);
	}

	@Override
	public List<LessonClass> findLessonClassByUserId(@RequestParam("userId")String userId) {
		return lessonClassService.findLessonClassByUserId(userId);
	}

	@Override
	public Lesson selectLessonById(@RequestParam("id")String id) {
		return lessonService.selectByPrimaryKey(id);
	}

	@Override
	public List<Lesson> findSubjectLesson(			
			@RequestParam("lessonClassId") String lessonClassId,
			@RequestParam("subjectId") String subjectId) {
		return lessonService.findSubjectLesson(lessonClassId, subjectId);
	}

	@Override
	public LessonClass findLessonClassById(String id) {
		return lessonClassService.selectByPrimaryKey(id);
	}

	@Override
	public LessonClass findLessonClassByCode(String code) {
		return lessonClassService.selectByCode(code);
	}


	public List<LessonClassUserRelation> findLesClsUseRelByClassId(
			@RequestParam("classId")String classId,
			@RequestParam("type")String type
			) {
		LessonClassUserRelation record=new LessonClassUserRelation();
		record.setLessonClassId(classId);
		record.setType(type);
		List<LessonClassUserRelation> list=lesUseRelService.findLessClsUserRelation(record);		
		return list;
	}

	@Override
	public LessonClassUserRelation selectLesClsUseRel(
			@RequestParam("classId")String classId,
			@RequestParam("userId")String userId,
			@RequestParam("type")String type) {
		LessonClassUserRelation record=new LessonClassUserRelation();
		record.setLessonClassId(classId);
		record.setType(type);
		record.setUserId(userId);
		List<LessonClassUserRelation> list=lesUseRelService.findLessClsUserRelation(record);	
		if(list.size()>0){
			return list.get(0);
		}
		return null;
	}


	@Override
	public List<LessonResourceRuleFrame> findFrameByLessonId(@RequestParam("lessonId")String lessonId) {
		return lessonResourceRuleFrameService.findFrameByLessonId(lessonId);
	}

	@Override
	public List<Lesson> findByParentId(String parentId) {
		return lessonService.findByParentId(parentId);
	}

	@Override
	public LessonClass selectLastFinishClass(@RequestParam("lessonId") String userId) {
		LessonClassUserRelation rel=lesUseRelService.selectLastFinishClass(userId);
		if(rel==null){
			return null;
		}
		LessonClass _class=lessonClassService.selectByPrimaryKey(rel.getLessonClassId());
		return _class;
	}

	@Override
	public List<Lesson> findLessLesson(@RequestParam("lessonClassId") String lessonClassId,@RequestParam("subjectId") String subjectId,@RequestParam("sort") Long sort) {
		return lessonService.findLessLesson(lessonClassId, subjectId, sort);
	}

	@Override
	public List<LessonClassUserRelation> findTeacherClass(@RequestParam("userId")String userId) {
		LessonClassUserRelation record=new LessonClassUserRelation();
		record.setUserId(userId);
		List<LessonClassUserRelation> list=lesUseRelService.findLessClsUserRelation(record);	
		return list;
	}
}
